<style>
    .er-a-table {
        width: 100%;
        border-collapse: collapse;
        margin: 10px 0;
    }
    .er-a-table th,
    .er-a-table td {
        padding: 8px;
        border: 1px solid #ddd;
        text-align: left;
    }
    .er-a-table th {
        background-color: #f5f5f5;
        font-weight: bold;
    }
    .er-a-table tr:hover {
        background-color: #f9f9f9;
    }
</style>
<p>审核期间不计费；为避免提交频繁，审核通过后的再次修改进入审核期间的也纳入计费2233；</p>
{if !empty($ad_list)}
    <table class="er-a-table">
        <thead>
            <tr>
                <th>广告标题</th>
                <th>广告链接</th>
                <th>广告图片</th>
                <th>序号</th>
                <th>状态</th>
                <th>过期时间</th>
                <th>累计租赁积分</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {loop $ad_list $item}
                <tr>
                    <td>{$item[title]}</td>
                    <td><a href="{$item[url]}" target="_blank">{$item[url]}</a></td>
                    <td><img src="{$item[image]}" alt="广告图片" style="max-width: 100px;"></td>
                    <td>{$item[sort]}</td>
                    <td>{if $item[status] == 1}已通过{elseif $item[status] == 3}待审核{elseif $item[status] == 4}修改后待审核{else}已过期{/if}</td>
                    <td>
                        <span class="expire-time" data-expire-time="{$item['expire_time']}"></span>
                    </td>
                    <td>{$item[rent_credit]}</td>
                    <td>
                        <button id="renew_btn_image" data-ad-id="{$item['id']}">续期</button>
                        <button id="edit_btn_image" data-ad-id="{$item['id']}">编辑</button>
                        <button id="delete_btn_image" data-ad-id="{$item['id']}">删除</button>
                    </td>
                </tr>
            {/loop}
        </tbody>
    </table>
{else}
    <p>暂无广告数据</p>
{/if}

<script>
document.addEventListener('DOMContentLoaded', function() {
    // 计算并显示剩余时间
    const expireElements = document.querySelectorAll('.expire-time');
    expireElements.forEach(el => {
        const expireTime = parseInt(el.dataset.expireTime);
        const now = Math.floor(Date.now() / 1000);
        const secondsLeft = expireTime - now;

        if (secondsLeft > 0) {
            let timeLeft = '';
            const daysLeft = Math.floor(secondsLeft / 86400);
            const hoursLeft = Math.floor((secondsLeft % 86400) / 3600);
            const minutesLeft = Math.floor((secondsLeft % 3600) / 60);

            if (daysLeft > 365) {
                const yearsLeft = Math.floor(daysLeft / 365);
                const remainingDays = daysLeft % 365;
                timeLeft = yearsLeft + '年' + (remainingDays > 0 ? remainingDays + '天' : '');
            } else if (daysLeft > 30) {
                const monthsLeft = Math.floor(daysLeft / 30);
                const remainingDays = daysLeft % 30;
                timeLeft = monthsLeft + '个月' + (remainingDays > 0 ? remainingDays + '天' : '');
            } else if (daysLeft > 0) {
                timeLeft = daysLeft + '天' + (hoursLeft > 0 ? hoursLeft + '小时' : '');
            } else if (hoursLeft > 0) {
                timeLeft = hoursLeft + '小时' + (minutesLeft > 0 ? minutesLeft + '分钟' : '');
            } else {
                timeLeft = minutesLeft + '分钟';
            }
            el.textContent = '还剩' + timeLeft + '过期';
        } else {
            el.textContent = '已过期';
        }
    });

    // 添加删除按钮事件监听
    document.querySelectorAll('#delete_btn_image').forEach(btn => {
        btn.addEventListener('click', function() {
            const adId = this.dataset.adId;
            if (confirm('确定要删除这条广告吗？')) {
                fetch('plugin.php?id=erling_gridad_image:route&er_name=gridad&action=delete&ad_id=' + adId, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body: 'formhash={FORMHASH}'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert('删除失败：' + data.message);
                    }
                })
                .catch(() => {
                    alert('网络请求失败，请稍后重试');
                });
            }
        });
    });
    // 添加编辑按钮事件监听
    document.querySelectorAll('#edit_btn_image').forEach(btn => {
        btn.addEventListener('click', function() {
            const adId = this.dataset.adId;
            showAddAdDialog_image(adId, 'edit');
        });
    });
    // 添加续期按钮事件监听
    document.querySelectorAll('#renew_btn_image').forEach(btn => {
        btn.addEventListener('click', function() {
            const adId = this.dataset.adId;
            showAddAdDialog_image(adId, 'renew');
        });
    });
    
});
</script>